<?php
// $Id: DateValue.class.php 73 2007-02-12 03:45:43Z tswicegood $
/**
 * This file contains {@link DateValue} which is part of the PHP Content 
 * Repository (phpCR), a derivative of the Java Content Repository JSR-170, and 
 * is licensed under the Apache License, Version 2.0.
 *
 * This file is based on the code created for
 * {@link http://www.jcp.org/en/jsr/detail?id=170 JSR-170}
 *
 * @author Travis Swicegood <travis@domain51.net>
 * @copyright PHP Code Copyright &copy; 2004-2005, Domain51, United States
 * @copyright Original Java and Documentation 
 *  Copyright &copy; 2002-2004, Day Management AG, Switerland
 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, 
 *      Version 2.0
 * @package phpContentRepository
 */

/**
 * Load the {@link phpCR} library file(s)
 */ 
require_once(dirname(__FILE__) . '/phpCR.library.php');
phpCR::loadAbstract('BaseValue');
phpCR::loadClass('PropertyType');


/**
 * A {@link DateValue} provides an implementation of the {@link Value} interface 
 * representing a date value.
 *
 * @author PHP - Travis Swicegood <travis@domain51.net>
 * @copyright Copyright &copy; 2004-2005, Domain51
 * @package phpContentRepository
 */
class DateValue extends BaseValue 
{
   /**
    * Defines the {@link PropertType} of this class.
    *
    * @var int
    * @see PropertyType::DATE
    */
    const TYPE = PropertyType::DATE;
    

   /**
    * Constructs a {@link DateValue} object representing a date.
    *
    * @param string
    *   The date this {@link DateValue} should represent
    *
    * @throws {@link ValueFormatException}
    *   If the $date is not a valid ISO8601-compliant date/time 
    *   string.
    */
    public function __construct($date)
    {
        parent::__construct(self::TYPE);
        if (is_string($date)) {
            $data = $date;
        } elseif ($date instanceof DateValue) {
            $data = $date->getData();
        }
        
        // This could be expanded upon to provide access to getYear(), 
        // getMonth(), getDay(), etc., type methods.
        $iso8601_pattern = '/^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(Z|[\-+][0-9]{2}:[0-9]{2})/';
        
        if (!preg_match($iso8601_pattern, $data)) {
            phpCR::loadException('ValueFormat');
            throw new ValueFormatException('Non-ISO8601 compliant date supplied');
        }
        
        $this->data = $data;
    }
    
    
   /**
    * Returns a new {@link DateValue} initialized to the value represented by 
    * the $s.
    *
    * The specified $s must be a ISO8601-compliant date/time
    * string.
    *
    * @param string
    *   The string to be parsed.
    * @return object
    *   A newly constructed {@link DateValue} representing the the specified 
    *   value.
    *
    * @throws {@link ValueFormatException}
    *   If the $s is not a valid ISO8601-compliant date/time 
    *   string.
    */
    static public function valueOf($s)
    {
        return new DateValue($s);
    }
    
    
   /**
    * Indicates whether some other object is "equal to" this one.
    *
    * The result is TRUE if and only if the argument is not
    * NULL and is a {@link DateValue} object that represents the
    * same value as this object.
    *
    * @param mixed
    *   The the value with which to compare.
    * @return  bool
    */
    public function equals($obj) 
    {
        if ($this == $obj) {
            return true;
        }
        
        if ($obj instanceof DateValue) {
            $objData = $obj->getData();
            
            if (is_null($objData)) {
                return false;
            }
            if ($this->data == $objData) {
                return true;
            }
        }
        
        return false;
    }
    
    
   /**
    * @see Value::getDate()
    */
    public function getDate()
    {
        $this->setValueConsumed();
        
        if (!is_null($this->data)) {
            return $this->data;
        } else {
            phpCR::loadException('ValueFormat');
            throw new ValueFormatException('empty value');
        }
    }
    
    
   /**
    * @see Value::getLong()
    */
    public function getInt()
    {
        $this->setValueConsumed();
        
        if (!is_null($this->data)) {
            $int = strtotime($this->data);
            if ($int === -1) {
                phpCR::loadException('ValueFormat');
                throw new ValueFormatException('Unable to determine time');
            }
            
            return $int;
        } else {
            phpCR::loadException('ValueFormat');
            throw new ValueFormatException('empty value');
        }
    }
    
    
   /**
    * @see Value::getBoolean()
    */
    public function getBoolean()
    {
        $this->setValueConsumed();
        
        if (!is_null($this->data)) {
            phpCR::loadException('ValueFormat');
            throw new ValueFormatException("cannot convert date to boolean");
        } else {
            phpCR::loadException('ValueFormat');
            throw new ValueFormatException('empty value');
        }
    }
}

